成长的架构
Rust 的集合,例如 Vec<T> 和 String,并非原始类型;它们是位于 std 模块中的库定义结构。这一基础决定了 Rust 如何通过模块系统组织数据,并通过 RAII(资源获取即初始化)机制管理内存。虽然简单类型存储在栈上,但集合使用 堆存储 实现动态增长,这意味着其内存必须通过 Drop 特性进行显式管理。
模块解析与可见性
Rust 编译器从项目根目录(src/lib.rs 或 src/main.rs)开始映射模块树。像 mod front_of_house; 这样的声明会触发编译器查找 src/front_of_house.rs 或 src/front_of_house/mod.rs。使用 pub 修饰符和重新导出(pub use)允许封装的堆分配数据通过符合惯例的路径安全地进行接口调用。
一旦模块的作用域结束, Drop 实现便会自动回收堆内存:$$Memory_{reclaimed} = \sum Drop(Elements)$$。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What happens if a project contains both
src/module.rs and src/module/mod.rs?The compiler prioritizes the directory style.
It triggers a compiler error due to ambiguity.
The files are merged automatically.
Rust ignores the .rs file.
✅ Correct!
Rust's module resolution rules forbid having both styles for the same module to avoid ambiguity.❌ Incorrect
The compiler cannot determine which file serves as the module root, resulting in an error.QUESTION 2
How does RAII ensure memory safety in collections?
By using a background garbage collector.
By checking references at runtime.
By tying heap memory to the scope of the owner variable.
By keeping all data on the stack.
✅ Correct!
RAII ensures that once the owner goes out of scope, resources are automatically freed.❌ Incorrect
Rust avoids garbage collection; RAII is a compile-time and scope-driven mechanism.QUESTION 3
Which file is considered the 'anchor' for module resolution in a library crate?
src/main.rs
$src/lib.rs$
mod.rs
Cargo.toml
✅ Correct!
In a library crate, src/lib.rs is the crate root where module trees begin.❌ Incorrect
src/main.rs is the root for binary crates, not library crates.QUESTION 4
What is the purpose of the
pub use statement?To allocate memory on the heap.
To re-export a name to provide a more idiomatic public API.
To private-label a module.
To increase compilation speed.
✅ Correct!
It allows you to flatten your module structure for external users while keeping internal organization clean.❌ Incorrect
Re-exporting doesn't change memory allocation; it changes visibility and path accessibility.QUESTION 5
Why can't Collections like
Vec be stored entirely on the stack?The stack cannot store integers.
They must have a fixed size known at compile time to stay on the stack.
The stack is too slow for collections.
Stack memory is never dropped.
✅ Correct!
Collections grow dynamically; the stack requires fixed sizes for stack frame calculation.❌ Incorrect
The stack is fast, but it is rigid; heap storage provides the necessary flexibility for growth.Architecting the Restaurant System
Module Resolution and Lifecycle Case Study
You are designing a restaurant management system. The waitlist is a heap-allocated collection managed inside a module named 'front_of_house'. You need to ensure the waitlist is accessible to the main restaurant logic but properly cleaned up after the dinner service (the scope) ends.
Q
If you move 'hosting' functions into their own file, what are the two valid path options for the hosting module file?
Solution:
The two valid paths are 'src/front_of_house/hosting.rs' or 'src/front_of_house/hosting/mod.rs'.
The two valid paths are 'src/front_of_house/hosting.rs' or 'src/front_of_house/hosting/mod.rs'.
Q
Explain how the 'Drop' trait prevents a memory leak if the waitlist vector is defined inside the 'hosting' module.
Solution:
When the 'front_of_house' module's owner goes out of scope, Rust calls the 'Drop' implementation for the waitlist vector. This automatically deallocates the heap memory used by the vector elements, ensuring no data 'outlives' its owner.
When the 'front_of_house' module's owner goes out of scope, Rust calls the 'Drop' implementation for the waitlist vector. This automatically deallocates the heap memory used by the vector elements, ensuring no data 'outlives' its owner.